package com.up.upzl.biz.repository;

import java.util.List;

import javax.transaction.Transactional;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import com.up.upzl.biz.domain.Captcha;
import com.up.upzl.biz.domain.Captcha.CodeType;

/**
 * 验证码管理
 * 
 * @author joker
 *
 */
/**
 * @author geeks
 *
 */
public interface CaptchaRepository extends JpaRepository<Captcha, Long> {

	/**
	 * 跟进手机号和验证码类型将所有验证码设置为失效
	 * 
	 * @param phone
	 *            手机号
	 * @param codeType
	 *            验证码类型
	 * @return
	 */
	@Transactional
	@Modifying(clearAutomatically = true)
	@Query("update Captcha captcha set captcha.status = false where captcha.phone=?1 and captcha.codeType=?2 ")
	Integer updateAllStatusByPhone(String phone, CodeType codeType);

	/**
	 * 获取当前验证码
	 * 
	 * @param phone
	 *            手机号
	 * @param codeType
	 *            验证码类型
	 * @return
	 */
	@Query("select captcha From Captcha captcha where captcha.phone=?1 and captcha.codeType=?2 and status = true")
	Captcha findCurrCode(String phone, String code);

	@Query("select captcha From Captcha captcha where captcha.phone=?1 and captcha.code=?2")
	Captcha findCode(String ihone, String code);

	List<Captcha> findByOpenid(String openid);
}
